package com.qf.smartdevice.controller;

import com.github.pagehelper.PageInfo;
import com.qf.smartdevice.constans.ResultCode;
import com.qf.smartdevice.dto.R;
import com.qf.smartdevice.exceptions.UpdateException;
import com.qf.smartdevice.pojo.Categories;
import com.qf.smartdevice.service.ICategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RestController
@RequestMapping("/categories")
public class CategoriesController {
    @Autowired
    private ICategoriesService categoriesService;

    @GetMapping("/categories")
    public R findAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, String categoryname, @RequestParam(defaultValue = "-100") Long status) {

        PageInfo<Categories> all = categoriesService.findAll(page, limit, categoryname, status);

        return R.setOk(all);

    }

    @PostMapping("/category")
    public R addCategories(@RequestBody Categories categories) {

        categoriesService.addCategories(categories);
        return R.setOk(null);
    }

    @DeleteMapping("/categories")
    public R deleteCategories(Integer[] ids) {
        System.out.println("删除的是" + ids);
        int i = categoriesService.deleteCategories(ids);
        return R.setOk(null);
    }

    @PutMapping("/category")
    public R updateById(@RequestBody Categories categories) {

        if (categories == null || categories.getId() == null || categories.isNull()) {
            throw new UpdateException("没有更新的数据", ResultCode.NULL_DATA);
        }

        int i = categoriesService.updateById(categories);
        return R.setOk(null);

    }

}
